#include<stdlib.h>
#include<iostream>
#include<fstream>
#include<sstream>
#include<vector>
#include<classify.h>
#include<classify.h>
#include<clutil.h>

unsigned nInstances;
unsigned nAttributes;
unsigned* database;

void
fileRead(const char* file)
{
	ifstream fp;
	fp.open(file, ios::in);
    if (!fp.is_open()) {
        cout <<  "Not Able to Open file " << file << endl;
        exit(0);
    }
    bool firstIter = true;
    // counting number of insatances and number of attributes
    while(!fp.eof()) {
        string line;
        getline(fp, line);
        if (line[0] == '\0') break; 
        stringstream s(line);
        unsigned dummy; 
        while (!s.eof() && firstIter) {
            s >> dummy;
            ++nAttributes; 
        }  
        ++nInstances; 
        firstIter = false;   
    } 
    nAttributes -= 1;
    cout << "Instances: " << nInstances << endl;
    cout << "Attributes: " << nAttributes <<  endl;
    unsigned* database = (unsigned*)allocateHostMemory
                         (sizeof(unsigned) * nInstances * nAttributes);
    fp.close();
    fp.open(file, ios::in);
    unsigned n = 0;
	while(!fp.eof()) {
		string line;
		getline(fp, line);
		if (line[0] == '\0') break;
        stringstream s(line);
        for (unsigned i = 0; i < nAttributes; ++i) {  
            s >> database[i * nInstances + n];          
            ++i;
        }
        firstIter = false;
        ++n;
    }
    fp.close();
}    
